package org.clockworkmages.games.anno1186.scripting.tools;

import org.clockworkmages.games.anno1186.model.effect.Effect;

public class ScriptBuilder {

	public static final String LEVEL = "level";

	protected StringBuffer sb;

	protected BuilderWrapper parent;

	public ScriptBuilder() {
		this.sb = new StringBuffer("");
	}

	public ScriptBuilder(StringBuffer sb) {
		this.sb = sb;
	}

	public ScriptBuilder(BuilderWrapper parent) {
		this();
		this.parent = parent;
	}

	public ScriptBuilder(StringBuffer sb, BuilderWrapper parent) {
		this(sb);
		this.parent = parent;
	}

	public static MeScriptBuilder ME() {
		return new MeScriptBuilder();
	}

	public static UtilScriptBuilder UTIL() {
		return new UtilScriptBuilder();
	}

	public static PlotScriptBuilder PLOT() {
		return new PlotScriptBuilder();
	}

	public static ScriptBuilder NOT() {
		StringBuffer sb = new StringBuffer("!");
		return new ScriptBuilder(sb);
	}

	public ScriptBuilder not() {
		sb.append("!");
		return this;
	}

	public static ScriptBuilder NEW() {
		StringBuffer sb = new StringBuffer("");
		return new ScriptBuilder(sb);
	}

	public MeScriptBuilder me() {
		return new MeScriptBuilder(sb, parent);
	}

	public EnemyScriptBuilder enemy() {
		return new EnemyScriptBuilder(sb, parent);
	}

	public UtilScriptBuilder util() {
		return new UtilScriptBuilder(sb, parent);
	}

	public PlotScriptBuilder plot() {
		return new PlotScriptBuilder(sb, parent);
	}

	public SituationScriptBuilder situation() {
		return new SituationScriptBuilder(sb, parent);
	}

	public ScriptBuilder gt() {
		sb.append(">");
		return this;
	}

	public ScriptBuilder gt(int value) {
		sb.append(">" + value);
		return this;
	}

	public ScriptBuilder ge() {
		sb.append(">=");
		return this;
	}

	public ScriptBuilder ge(int value) {
		sb.append(">=" + value);
		return this;
	}

	public ScriptBuilder lt() {
		sb.append("<");
		return this;
	}

	public ScriptBuilder lt(int value) {
		sb.append("<" + value);
		return this;
	}

	public ScriptBuilder le() {
		sb.append("<=");
		return this;
	}

	public ScriptBuilder le(int value) {
		sb.append("<" + value);
		return this;
	}

	public ScriptBuilder equals(String value) {
		sb.append("==" + value);
		return this;
	}

	public ScriptBuilder equalsString(String value) {
		sb.append("=='" + value + "'");
		return this;
	}

	public ScriptBuilder append(String value) {
		sb.append(value);
		return this;
	}

	public ScriptBuilder and() {
		sb.append(" && ");
		return this;
	}

	@Override
	public String toString() {
		return sb.toString();
	}

	public ScriptBuilder iff() {
		return parent.iff();
	}

	public ScriptBuilder then(String s) {
		parent.then(s);
		return this;
	}

	public ScriptBuilder elsee(String s) {
		parent.elsee(s);
		return this;
	}

	public ScriptBuilder elseIf() {
		return parent.elseIf();
	}

	public BuilderWrapper getParent() {
		return parent;
	}

	public String asText() {
		return parent.toString();
	}

	public Effect compileEffect() {
		return ((EffectBuilder) parent).compile();
	}

	public String asPlaceholder() {
		return "${" + sb.toString() + "}";
	}

}
